#coding=utf-8
import os
import sys
import datetime


def script_path():
    import inspect, os
    caller_file = inspect.stack()[1][1]         # caller's filename
    return os.path.abspath(os.path.dirname(caller_file))# path
    
lc = script_path()
parent = os.path.dirname(lc)

decodesArray = ['GB2312', 'GBK', 'Windows-1252', 'cant15-22', 'cp936', 'utf-8', 'ascii', 'unicode']
import chardet
def chang_line(line):
    line2 = str(line)
    tmp = chardet.detect(line2.encode())
    language = tmp.get("encoding")
    #print("tmp:{}".format(tmp))
    if language == 'utf-8':
        return line2
    for i in decodesArray:
        try:
            return str(line.decode(i).encode('utf-8'))
        except:
            continue
    try:
        return str(line.decode(language).encode('utf-8'))
    except:
        print("tmp:{}".format(tmp))        
        return line2
    '''
    try:
        return str(line.decode('GB2312').encode('utf-8'))
    except:
        try:
            return str(line.decode('GBK').encode('utf-8'))
        except:
            try:
                return str(line.decode('Windows-1252').encode('utf-8'))
            except:
                try:
                    return str(line.decode('GBK').encode('utf-8'))
                except:
                    try:
                        return str(line.decode(language).encode('utf-8'))
                    except:
                        try:
                            return str(line.decode('cant15-22').encode('utf-8'))
                        except:
                            try:
                                return str(line.decode('cp936').encode('utf-8'))
                            except:
                                return str(line.decode('utf-8').encode('utf-8'))
    '''
    '''
    if language == 'ascii':
        return str(line.decode(language,'strict').encode('utf-8'))
    if language == 'Windows-1252':
        return str(line.decode('Windows-1252').encode('utf-8'))
    if language == 'gbk':
        return str(line.decode('gbk').encode('utf-8'))
    return line2
    '''

index = 1    
def list_cpp(path, lc, deep):
    global index
    full_path =  lc + "/" + path;
    if path == "":
        full_path = lc
    ls = os.listdir(full_path)
    for i in ls:
        child_path =  full_path + "/" + i
        if os.path.isfile(child_path) == False:
            if deep == 2:
                ls2 = os.listdir(child_path)
                fileName = parent + "/novel/" + str(index) +" " + i + ".txt"
                if os.path.exists(fileName):
                    os.remove(fileName)
                ff = open(fileName, "w+")
                index += 1
                begin = datetime.datetime.now()
                print("{} begin {}".format(i, begin.strftime('%Y-%m-%d  %H:%M:%S')))
                for x in ls2:
                    child_path_2 =  child_path + "/" + x
                    if os.path.isfile(child_path_2) == True: 
                        f = open(child_path_2, 'rb')
                        for line in f:
                            ff.write(chang_line(line))
                        f.close()
                ff.close()
                end = datetime.datetime.now()
                print("{} success {}\t{}ms".format(i,  datetime.datetime.now().strftime('%Y-%m-%d  %H:%M:%S'), int((end-begin).microseconds/1000)))
                continue
            list_cpp(path + "/" + i, lc, deep + 1)

list_cpp("", lc, 1)